<!--
 * @Date: 2022-12-07 14:57:46
 * @LastEditTime: 2022-12-22 11:29:03
 * @Description: content
-->
<template>
  <view class="page-wrap">
    <uni-nav-bar
      title="订单中心"
      color="#333"
      leftIcon="left"
      backgroundColor="#F0DBB0;"
      :border="false"
      :statusBar="true"
      :fixed="true"
      @clickLeft="$common.back()"
    >
      <template v-slot:left>
        <view @click="$common.back()">
          <image
            src="/static/images/back.png"
            style="width: 32rpx; height: 32rpx"
            mode="scaleToFill"
          />
        </view>
      </template>
    </uni-nav-bar>

    <view
      class="tab-list"
      :style="{
        top: `calc(${sysConfig.statusBarHeight}px + 44px)`,
      }"
    >
      <view
        @click="tabChage(i)"
        v-for="(item, i) in tabList"
        :key="i"
        class="tab-list-item"
        :class="{
          act: tabCur == i,
        }"
      >
        <img
          :src="item.img"
          alt=""
          style="width: 100%; height: 100%"
          srcset=""
        />
      </view>
    </view>
    <u-gap height="88"></u-gap>
    <mescroll-body
      ref="mescrollRef"
      @init="mescrollInit"
      @down="downCallback"
      @up="getList"
      :down="downOption"
      :up="upOption"
    >
      <view class="list">
        <view v-for="(item, i) in 5" :key="i" class="list-item">
          <view class="list-item-hd">
            <view class="pic">
              <cimage :src="item.thumb" mode="scaleToFill" />
            </view>

            <view class="info">
              <view class="title hang2">
                {{ item.title || "商品名" }}
              </view>

              <view class="order-time">
                时间: {{ item.create_time || "2025-03-22 20:30" }}
              </view>

              <view class="price-num">
                <view class="price">价格: ¥{{ item.price || "11.30" }}</view>

                <view class="quantity-control">
                  <text class="minus">-</text>
                  <input
                    type="number"
                    class="quantity"
                    :value="item.num || 1"
                    disabled
                  />
                  <text class="plus">+</text>
                </view>
              </view>
            </view>
          </view>

          <view class="list-item-bd">
            <view v-if="tabCur == 0" class="countdown">
              <image src="/static/images/djs.png" class="countdown-icon" mode="scaleToFill" />
              <text class="countdown-time">00:27:47 后订单关闭</text>
            </view>
            <view class="payment-amount">
              实付金额:
              <text class="amount">{{ item.points || 6900 }}</text>
              <text class="unit">积分</text>
            </view>
          </view>

          <view class="list-item-ft">
            <template v-if="tabCur == 0">
              <view @click="toDetail(item)" class="btn cancel">取消订单</view>
              <view @click="toDetail(item)" class="btn service">联系客服</view>
              <view @click="toDetail(item)" class="btn detail">查看详情</view>
            </template>

            <template v-if="tabCur == 1">
              <view @click="toDetail(item)" class="btn service">联系客服</view>
              <view @click="toExpress(item)" class="btn detail">查看物流</view>
              <view @click="openModel(item)" class="btn confirm">确认收货</view>
            </template>

            <template v-if="tabCur == 2">
              <view class="status-tag">已完成</view>
              <view @click="toDetail(item)" class="btn detail">查看详情</view>
            </template>
          </view>
        </view>
      </view>
    </mescroll-body>

    <u-modal
      v-model="showModel"
      :title="'提示'"
      :content="'请确认是否已收到货'"
      :show-cancel-button="true"
      @confirm="submitGet"
    ></u-modal>
  </view>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      // 下拉刷新的配置(可选, 绝大部分情况无需配置)
      downOption: {
        auto: false,
      },
      // 上拉加载的配置(可选, 绝大部分情况无需配置)
      upOption: {
        auto: true,
        page: {
          size: 20, // 每页数据的数量,默认10
        },
      },
      listData: [],
      tabList: [
        {
          id: 1,
          title: "待发货",
          img: "/static/images/order_all.png",
        },
        {
          id: 2,
          title: "待收货",
          img: "/static/images/order_yqx.png",
        },
        {
          id: 4,
          title: "已完成",
          img: "/static/images/order_dfk.png",
        },
        {
          id: 4,
          title: "已完成",
          img: "/static/images/order_dsh.png",
        },

        {
          id: 4,
          title: "已完成",
          img: "/static/images/order_ywc.png",
        },
      ],
      tabCur: 0,
      canRefresh: false,
      curItem: "",
      showModel: false,
    };
  },

  computed: {
    ...mapGetters(["sysConfig"]),
  },

  onShow() {
    this.canRefresh && this.mescroll.resetUpScroll();
    this.canRefresh && this.mescroll.scrollTo(0, 0);
    this.canRefresh = true;
  },

  methods: {
    /**
     * @description: 打开确认收货弹窗
     * @param {*} item
     * @return {*}
     */
    openModel(item) {
      this.curItem = item;
      this.showModel = true;
    },

    /**
     * @description: 确认收货
     * @param {*}
     * @return {*}
     */
    submitGet() {
      this.req({
        url: "/v1/shop/ship/receipt",
        data: {
          id: this.curItem.id,
        },
        success: (res) => {
          if (res.code == 200) {
            this.$common.toast({
              title: res.msg,
              icon: "success",
              duration: 600,
              success: () => {
                this.mescroll.resetUpScroll();
                this.mescroll.scrollTo(0, 0);
              },
            });
          }
        },
      });
    },

    /**
     * @description: 获取商品列表数据
     * @param {*}
     * @return {*}
     */
    getList({ num, size }) {
      this.req({
        url: "/v1/shop/ship/order",
        data: {
          page: num,
          per_page: size,
          status: this.tabList[this.tabCur].id,
        },
        Loading: true,
        success: (res) => {
          if (res.code == 200) {
            if (num == 1) {
              this.listData = [];
            }

            this.listData = [...this.listData, ...res.data.data];
            this.mescroll.endBySize(res.data.data.length, res.data.total);
          }
        },
      });
    },

    /**
     * @description: 物流详情
     * @return {*}
     */
    toExpress(item) {
		this.req({
			url:"/api/v1/pay/cancel",
		})
      // this.$common.to({
      //   url: "/package/mine/mall-express",
      //   query: {
      //     id: item.id,
      //   },
      // });
    },

    /**
     * @description: 订单详情
     * @return {*}
     */
    toDetail(item) {
      this.$common.to({
        url: "/package/mine/mall-order-detail",
        query: {
          id: item.id,
        },
      });
    },

    /**
     * @description: tab切换
     * @param {*} e
     * @return {*}
     */
    tabChage(e) {
      this.tabCur = e;
      this.listData = [];
      this.mescroll.resetUpScroll();
      this.mescroll.scrollTo(0, 0);
    },
  },
};
</script>

<style lang="scss">
.countdown-time {
  color: #000;
}
.page-wrap {
  background: #f0dbb0;
  .tab-list {
    height: 88rpx;
    padding: 0 30rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    z-index: 10;

    &-item {
      font-size: 28rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #999999;
      transition: all 0.3s;

      &.act {
        font-size: 32rpx;
        font-weight: bold;
        color: #333333;
      }
    }
  }

  .btn-list-wrap {
    width: 100%;
    height: 88rpx;
    position: fixed;
    z-index: 10;
    left: 0;
    box-sizing: border-box;
    padding: 0 30rpx;
    background: #fff;

    &-content {
      display: inline-block;
      height: 100%;

      .btn-list {
        display: flex;
        align-items: center;
        height: 100%;

        &-item {
          width: 122rpx;
          height: 60rpx;
          background: #f5f5f5;
          border-radius: 5rpx;
          line-height: 60rpx;
          text-align: center;
          box-sizing: border-box;
          border: 1rpx solid #f5f5f5;

          font-size: 26rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #333333;

          &.act {
            border-color: #333333;
            background: #fff;
          }
        }

        &-item + .btn-list-item {
          margin-left: 20rpx;
        }
      }
    }
  }

  .list {
    &-item {
      border-radius: 10rpx;
      background: #fff2d8;
      padding: 30rpx;
      margin-top: 30rpx;

      &-hd {
        display: flex;
        padding-bottom: 20rpx;

        .pic {
          width: 166rpx;
          height: 166rpx;
          box-sizing: border-box;
          border: 1rpx solid #f7f7f7;
          position: relative;
          border-radius: 10rpx;
          overflow: hidden;
        }

        .info {
          width: calc(100% - 166rpx);
          box-sizing: border-box;
          padding-left: 30rpx;
          display: flex;
          flex-flow: column nowrap;
          justify-content: space-between;

          .title {
            font-size: 28rpx;
            font-family: PingFang SC;
            font-weight: 500;
            color: #333333;
            margin-bottom: 8rpx;
          }

          .order-time {
            font-size: 24rpx;
            color: #999;
            margin-bottom: 10rpx;
          }

          .price-num { 
            display: flex;
            justify-content: space-between;
            align-items: center;

            .price {
              font-size: 28rpx;
              font-family: PingFang SC;
              font-weight: 500;
              color: #e82929;
            }

            .quantity-control {
              display: flex;
              align-items: center;
              border: 1px solid #e8e8e8;
              border-radius: 6rpx;
              overflow: hidden;
              background: #f7f7f7;

              .minus,
              .plus {
                width: 60rpx;
                height: 60rpx;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 28rpx;
                color: #666;
                background: #f7f7f7;
              }

              .quantity {
                width: 80rpx;
                height: 60rpx;
                text-align: center;
                font-size: 28rpx;
                background: #fff;
                border-left: 1px solid #e8e8e8;
                border-right: 1px solid #e8e8e8;
              }
            }
          }
        }
      }

      &-bd {
        padding: 20rpx 0;
        position: relative;

        .countdown {
          display: flex;
          align-items: center;
          background-color: #f9e9c3;
          padding: 12rpx 30rpx;
          border-radius: 8rpx;
          width: 100%;
          box-sizing: border-box;
          margin-bottom: 10rpx;

          &-icon {
            margin-right: 10rpx;
            width: 40rpx;
            height: 40rpx;
          }

          &-time {
            font-weight: 500;
            font-size: 28rpx;
            color: #333333;
          }
        }

        .payment-amount {
          font-size: 26rpx;
          color: #333;
          font-weight: 500;
          text-align: right;

          .amount {
            font-size: 30rpx;
            font-weight: bold;
          }

          .unit {
            font-size: 24rpx;
            color: #666;
            margin-left: 4rpx;
          }
        }
      }

      &-ft {
        padding-top: 20rpx;
        border-top: 1rpx solid #f7f7f7;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        flex-wrap: nowrap;

        .status-tag {
          color: #4caf50;
          font-size: 24rpx;
          margin-right: auto;
        }

        .btn {
          border-radius: 8rpx;
          box-sizing: border-box;
          line-height: 60rpx;
          text-align: center;
          font-size: 28rpx;
          font-family: PingFang SC;
          font-weight: 500;
          padding: 0 20rpx;
          border: 5rpx solid #000;

          &.cancel {
            width: 166rpx;
            background: #f5d685;
            color: #333;
            border: 5rpx solid #000;
          }

          &.service {
            width: 166rpx;
            background: #f5d685;
            color: #333;
            border: 5rpx solid #000;
          }

          &.detail {
            width: 166rpx;
            background: #e74c3c;
            color: #fff;
          }

          &.confirm {
            background: #4caf50;
            color: #fff;
          }
        }

        .btn + .btn {
          margin-left: 20rpx;
        }
      }
    }
  }
}
</style>
